/**
 * 
 */
package captor.windowsystem.project.newproject.cards;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import captor.crosscuttingdomainsystem.CrosscuttingDomain;
import captor.crosscuttingdomainsystem.CrosscuttingDomainSystem;
import captor.crosscuttingdomainsystem.crosscuttinginfo.DomainInfo;
import captor.crosscuttingdomainsystem.crosscuttinginfo.DomainInfoType.CompatibleDomainsType;
import captor.crosscuttingdomainsystem.crosscuttinginfo.DomainInfoType.CompatibleDomainsType.DomainType;
import captor.crosscuttingdomainsystem.crosscuttinginfo.impl.DomainInfoImpl;
import captor.crosscuttingdomainsystem.crosscuttinginfo.impl.DomainInfoTypeImpl;
import captor.domainsystem.DomainException;
import captor.domainsystem.DomainSystem;
import captor.lib.intl.MyIntl;
import captor.modelsystem.CaptorDomain;
import captor.modelsystem.Model;
import captor.modelsystem.Project;
import captor.windowsystem.util.DomainCheckBox;

/**
 * @author Carlos
 *
 */

public class ConfigNewDomainInfo extends FITCard implements ActionListener {

	
	/**
	 * 
	 */
	private static final long serialVersionUID = -9035848541698385885L;
	private JPanel cardHeader;
	private JPanel cardBody;
	private Vector<DomainCheckBox> vectorDomainCheckBox;
	private JPanel pnlCompatibleDomains;
	
	private JRadioButton rdoCrosscuttingDomain;
	private JRadioButton rdoBaseDomain;
	private ButtonGroup myButtonGroup;

	public ConfigNewDomainInfo(Model model, Project project){
		super(model,project);
	}
	
	/* (non-Javadoc)
	 * @see captor.windowsystem.project.newproject.cards.FITCard#create()
	 */
	@Override
	public void create() {
		// TODO Auto-generated method stub
		
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		add(Box.createRigidArea(new Dimension(5,20)));
		
		cardHeader = createCardHeader();
        add(cardHeader);
        
        add(Box.createRigidArea(new Dimension(5,20)));
            
        cardBody = createCardBody();
        add(cardBody);                        
	}

	/* (non-Javadoc)
	 * @see captor.windowsystem.project.newproject.cards.FITCard#validateFields()
	 */
	@Override
	public boolean validateFields() {
		// TODO Auto-generated method stub
		return true;
	}

	private JPanel createCardHeader(){
		
		JLabel lblChoice;
		Color c = Color.BLUE;
        c = c.darker();				       
        
        lblChoice = new JLabel(MyIntl.NEW_PROJECT_WINDOW_DOMAIN_TYPE);
        lblChoice.setFont(new Font("Default", Font.BOLD, 12));
        lblChoice.setForeground(c);
                       
        JPanel header = new JPanel();                   
        header.setLayout(new BoxLayout(header, BoxLayout.X_AXIS));
        header.add(lblChoice);        
        header.add(Box.createHorizontalGlue());
        
        return header;
	}
	
	private JPanel createCardBody(){
		
		JPanel body = new JPanel();
		
		body.setLayout(new BoxLayout(body, BoxLayout.PAGE_AXIS));		
				
		rdoCrosscuttingDomain = new JRadioButton(MyIntl.MSG125,true);
		rdoBaseDomain = new JRadioButton(MyIntl.MSG123);
		
		rdoCrosscuttingDomain.setAlignmentX(JRadioButton.CENTER_ALIGNMENT);
		rdoBaseDomain.setAlignmentX(JRadioButton.CENTER_ALIGNMENT);
		
		rdoBaseDomain.setSize(rdoCrosscuttingDomain.getSize());
		rdoBaseDomain.setPreferredSize(rdoCrosscuttingDomain.getPreferredSize());
		rdoBaseDomain.setMaximumSize(rdoCrosscuttingDomain.getMaximumSize());
		
		//Create a ButtonGroup object, add buttons to the group
		myButtonGroup = new ButtonGroup();
		myButtonGroup.add(rdoCrosscuttingDomain);
		myButtonGroup.add(rdoBaseDomain);	
				
		body.add(rdoCrosscuttingDomain);
		body.add(rdoBaseDomain);
		body.add(Box.createRigidArea(new Dimension(5,20)));
		
		pnlCompatibleDomains = createPanelCompatibleDomain();
		body.add(pnlCompatibleDomains);
																
		rdoCrosscuttingDomain.addActionListener(this);
		rdoBaseDomain.addActionListener(this);
		
		return body;
	}

	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
		if ( e.getActionCommand().equals(MyIntl.MSG123) )  {			
			pnlCompatibleDomains.setVisible(rdoBaseDomain.isSelected());
		}
		else if ( e.getActionCommand().equals(MyIntl.MSG125) ){
			pnlCompatibleDomains.setVisible(!rdoCrosscuttingDomain.isSelected());
		}		
	}
	
	private DomainInfo getDomainInfo(){
		DomainInfo domainInfo = new DomainInfoImpl();
		
		domainInfo.setCrosscuttingDomain(rdoCrosscuttingDomain.isSelected());
		
		if (rdoBaseDomain.isSelected()){
			CompatibleDomainsType compatibleDomains = new DomainInfoTypeImpl.CompatibleDomainsTypeImpl();
			
			for (DomainCheckBox item : vectorDomainCheckBox ){
				if (item.isSelected()){
					DomainType domain = new DomainInfoTypeImpl.CompatibleDomainsTypeImpl.DomainTypeImpl();
					String domainName = item.getCrosscuttingDomain().getNameDomain();
					domain.setDomainName(domainName);
					compatibleDomains.getDomain().add(domain);
				}
			}
			domainInfo.setCompatibleDomains(compatibleDomains);
		}		
		return domainInfo;
	}
	
	public CaptorDomain getDomain(){
		DomainSystem domainSystem = new DomainSystem(this.model);
		DomainInfo crosscuttingDomainInfo = getDomainInfo();		
		boolean flagXmlInfoFile = false;
		return domainSystem.getDomain(project.getName(), crosscuttingDomainInfo, flagXmlInfoFile);		
	}
	
	private JPanel createPanelCompatibleDomain(){
		
		JPanel pnlCompatibleDomains = new JPanel();
		pnlCompatibleDomains.setLayout(new BoxLayout(pnlCompatibleDomains, BoxLayout.LINE_AXIS));
		
		boolean flagXmlInfoFile = true;
		CrosscuttingDomainSystem cds = new CrosscuttingDomainSystem(this.model);
		List<CrosscuttingDomain> crosscuttingDomainsList= cds.getCrosscuttingDomains();
		
		JPanel pnlDomainsCheckBox = new JPanel();
		pnlDomainsCheckBox.setLayout(new BoxLayout(pnlDomainsCheckBox, BoxLayout.PAGE_AXIS));
		vectorDomainCheckBox = new Vector<DomainCheckBox>();
		DomainCheckBox item;
		
		for(CrosscuttingDomain crosscuttingDomain : crosscuttingDomainsList) {
		    try{
		    	crosscuttingDomain.validateDomain();
		    	item = new DomainCheckBox(crosscuttingDomain.getNameDomain(), crosscuttingDomain );
			    vectorDomainCheckBox.add(item);
		    }
		    catch (DomainException e1){
		    	String msg = MyIntl.MSG119;	
		    	msg = msg.concat(e1.getMessage());
		    	item = new DomainCheckBox(crosscuttingDomain.getNameDomain()+msg);
		    	item.setEnabled(false);
		    }		    		   
		    pnlDomainsCheckBox.add(item);
		}
		pnlCompatibleDomains.add(new JLabel (MyIntl.NEW_PROJECT_WINDOW_COMPATIBLE_DOMAINS));
		pnlCompatibleDomains.add(pnlDomainsCheckBox);
		pnlCompatibleDomains.setVisible(false);
		
		return pnlCompatibleDomains;
	}
}